# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html

import pymongo
import pymysql


class Jingdong1Pipeline_mongo(object):
    def __init__(self):
        myclient = pymongo.MongoClient('127.0.0.1', 27017)
        mydb = myclient['京东']
        if '手表' in mydb.list_collection_names():
            mydb.drop_collection('手表')
        self.mycol = mydb['手表']

    def process_item(self, item, spider):
        self.mycol.insert_one(dict(item))
        return item


class Jingdong1Pipeline_mysql(object):
    def __init__(self):
        self.db = pymysql.Connect(
            host='127.0.0.1',
            user='root',
            password='1111',
            db='jingdong',
            charset='utf8'
        )
        self.cur = self.db.cursor()

    def process_item(self, item, spider):
        sql = "insert shoubiao values(0,'%s','%s','%s','%s','%s')" % (
            item['title'], item['jiage'], item['image'], item['spid'], item['plnum'])
        self.cur.execute(sql)
        self.db.commit()
        return item

    def spider_close(self, item, spider):
        self.cur.close()
        self.db.close()
